package com.ctrl.riveradministrator.mvp.view.weight;

import android.app.Activity;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;

import me.relex.photodraweeview.OnPhotoTapListener;
import me.relex.photodraweeview.PhotoDraweeView;

/**
 * 查看图片的activity
 */
public class ImageViewActivity extends Activity {

    private PhotoDraweeView mPhotoDraweeView;
    private String img_url;
    private String type = "net";//URL为网络地址或本地地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
        initView();
        initData();
    }

    private void initView() {
        mPhotoDraweeView = findViewById(R.id.photoView);
    }

    private void initData() {
        img_url = getIntent().getStringExtra("img_url");
        type = getIntent().getStringExtra("type");
        if (!TextUtils.isEmpty(img_url)) {
            if (type.equals("local")) {
                mPhotoDraweeView.setImageURI("file://" + img_url);
            } else if (type.equals("net")) {
                PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
                controller.setUri(img_url);//设置图片url
                controller.setOldController(mPhotoDraweeView.getController());
                controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        super.onFinalImageSet(id, imageInfo, animatable);
                        if (imageInfo == null || mPhotoDraweeView == null) {
                            return;
                        }
                        mPhotoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                    }
                });
                if (TextUtils.isEmpty(img_url)) {
                    mPhotoDraweeView.setImageURI(Uri.parse("res:///" + R.mipmap.ic_launcher));
                }
                mPhotoDraweeView.setController(controller.build());
            }
        } else {
            Toast.makeText(this, "图片获取失败", Toast.LENGTH_SHORT).show();
        }
    }

    public void back(View view) {
        finish();
    }
}
